package riteservices.model.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;
import riteservices.model.bean.Request;
import riteservices.util.Constantes;

public class RequestDAO extends GeneralDAO<Request> {
 
	public String approveRequest(Request request) {
            if(update(request)){
                return Constantes.TRUE_STR;
            }else{
                return Constantes.FALSE_STR;
            }
	}
	 
	public String rejectRequest(Request request) {
            if(update(request)){
                return Constantes.TRUE_STR;
            }else{
                return Constantes.FALSE_STR;
            }
	}
        
        public Request getRequestById(int idRequest) {
//            List<Request> result=new ArrayList<Request>();
        
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Request.class);           
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            criteria.setFetchMode("round", FetchMode.JOIN);
            criteria.setFetchMode("userBySender", FetchMode.JOIN);
            criteria.setFetchMode("round.users", FetchMode.JOIN);
            criteria.setFetchMode("round.route", FetchMode.JOIN);
            criteria.add(Restrictions.eq("idRequest", idRequest));
            Request result = (Request) criteria.uniqueResult();
            session.close();        
            return result;
//            List<Request> requests = search("idRequest="+idRequest,Request.class);
//            if(!requests.isEmpty()){
//                return requests.get(0);
//            }else{
//                return null;
//            }
	}
	 
        public Request getRequestByStatus(int status) {
            List<Request> requests = search("status="+status,Request.class);
            if(!requests.isEmpty()){
                return requests.get(0);
            }else{
                return null;
            }
	}
        
        public List<Request> getRequestsUserReciver(int idUser, int idStatus){
            List<Request> result=new ArrayList<Request>();
        
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Request.class);
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            criteria.setFetchMode("userBySender", FetchMode.JOIN);
            criteria.setFetchMode("round", FetchMode.JOIN);
            criteria.setFetchMode("round.route", FetchMode.JOIN);
            criteria.add(Restrictions.eq("userByReciver.idUser", idUser));
            criteria.add(Restrictions.eq("status", idStatus));
            result = criteria.list();
            session.close();        
            return result;
//            List<Request> requests = search("status="+idStatus+" and reciver="+idUser,Request.class);
//            if(!requests.isEmpty()){
//                return requests;
//            }else{
//                return null;
//            }
        }
        
        public List<Request> getRequestsUserSender(int idUser, int idStatus){
            List<Request> result=new ArrayList<Request>();
        
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Request.class);
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            criteria.setFetchMode("round", FetchMode.JOIN);
            criteria.setFetchMode("round.route", FetchMode.JOIN);
            criteria.setFetchMode("round.user", FetchMode.JOIN);
            criteria.setFetchMode("round.user.ratingsForOwner", FetchMode.JOIN);
            criteria.add(Restrictions.eq("userBySender.idUser", idUser));
            criteria.add(Restrictions.eq("status", idStatus));
            result = criteria.list();
            session.close();        
            return result;
//            List<Request> requests = search("status="+idStatus+" and sender="+idUser,Request.class);
//            if(!requests.isEmpty()){
//                return requests;
//            }else{
//                return null;
//            }
        }
}
 
